package com.yq.tanhua.ui.friends

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Tab
import androidx.compose.material3.TabRow
import androidx.compose.material3.Text
import androidx.compose.runtime.*
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import com.google.accompanist.pager.ExperimentalPagerApi
import com.google.accompanist.pager.HorizontalPager
import com.google.accompanist.pager.rememberPagerState
import com.yq.tanhua.databinding.TanhuaFragmentFriendsBinding
import com.yq.tanhua.ui.FollowMovmentsPage
import com.yq.tanhua.ui.RecommendMovmentsPage
import com.yq.tanhua.ui.publish.PublishActivity
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import net.lucode.hackware.magicindicator.buildins.UIUtil

class FriendsFragment : Fragment() {

    private var _binding: TanhuaFragmentFriendsBinding? = null

    private val binding get() = _binding!!
    lateinit var viewModel: FriendsViewModel

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        viewModel =
            ViewModelProvider(this).get(FriendsViewModel::class.java)

        _binding = TanhuaFragmentFriendsBinding.inflate(inflater, container, false)
        val root: View = binding.root
        binding.compose.setContent {
            content()
        }
        binding.btnPublish.setOnClickListener {
            startActivity(Intent(context, PublishActivity::class.java))
        }

        return root
    }

    private val titles = listOf("关注", "推荐")

    @OptIn(ExperimentalPagerApi::class)
    @Composable
    public fun content() {
        val scopeState = rememberCoroutineScope()
        var state = rememberPagerState(initialPage = 0)
        Column {

            TabRow(
                selectedTabIndex = state.currentPage,
                containerColor = Color.White,
                modifier = Modifier.padding(top = 30.dp)
            ) {
                titles.forEachIndexed { index, title ->
                    Tab(
                        text = {
                            Text(
                                title,
                                color = if (state.currentPage == index) Color.Red else Color.Black
                            )
                        },
                        selected = state.currentPage == index,
                        onClick = {
                            scopeState.launch {
                                state.scrollToPage(index)
                            }
                        }
                    )
                }
            }

            HorizontalPager(
                count = titles.size,
                state = state
            ) { page ->
                when (page) {
                    0 -> FollowMovmentsPage(viewModel)
                    1 -> RecommendMovmentsPage(viewModel)
                }
            }
        }

    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }
}